'use strict';

app.controller('UserCtrl', 
['$scope','$http', 'dialogs', '$state', 'UserService',
function($scope, $http, dialogs, $state, UserService) {
	var vm = $scope.vm = this;
    /*	var defaultQuery = {searchType:"account"};
     vm.query = angular.copy(defaultQuery);*/
	
	vm.flexigridObj = {};	
	vm.options = UserService.getGridOptions();

	//查询
	vm.search = function(){
		var param = angular.copy(vm.query);
        /*param[param.searchType] = param.keyword;
		delete param.searchType;
         delete param.keyword;*/
		vm.flexigridObj.flexOptions({
			newp: 1,
			extParam: param
		}).flexReload();
	}

	// 重置
	vm.reset = function(){
        vm.query = angular.copy({});
		vm.search();
	}
	
	// 新增
	vm.add = function(){
		$state.go('setting.user.add');
	}
	// 修改
	vm.update = function(id){
		$state.go('setting.user.update', {id: id});
	}
	// 删除
	vm.del = function(index){
		var data = vm.flexigridObj.getRawRecords(index);
		// 弹出确认删除提示信息
		var dlg = dialogs.confirm(getMessage("common.deleteConfirm"));
		dlg.result.then(function(param){
			UserService.remove(data).then(function(data){
				// 刷新列表
				vm.flexigridObj.flexReload();
            	//弹出提示信息
            	dialogs.notify(data);
			});
		});
	}
	//解锁
	vm.unlock = function(){
		var datas = vm.flexigridObj.getCheckedRows();
		if(datas && datas.length > 0) {
			var ids = [];
			var accounts = [];
			angular.forEach(datas, function(user,index){
				ids.push(user.id);
				accounts.push(user.account);
			});
			var dlg = dialogs.confirm(getMessage("user.querenjiesuo"));
			dlg.result.then(function(param){
				UserService.unlock(ids.join(','), accounts.join(',')).then(function(data){
					// 刷新列表
					vm.flexigridObj.flexReload();
	            	//弹出提示信息
	            	dialogs.notify(data);
				});
			});
		}else{
			dialogs.notify(getMessage("common.plzSelectOneRecourd"));
		}
	}
	//重置密码
	vm.resetPwd = function(index){
		var data = vm.flexigridObj.getRawRecords(index);
		UserService.resetPwd(data);
	}
	
}]);

app.controller('UserAddOrUpdateCtrl', ['$scope', '$http', '$state', 'dialogs', 'UserService', 'DynamicI18n',
    function ($scope, $http, $state, dialogs, UserService, DynamicI18n) {
	var vm = $scope.vm = this;
	vm.user = {id:''};
	vm.isEdit = false;
	vm.selectRoles = [];
	vm.pwdRule = angular.copy($scope.app.pwdRule);
	
	function cancel(){
		$state.go('setting.user.list');
	}
	//角色列表
	vm.roleList = [];
        //从数据字典获取所属机构
        vm.departments = DynamicI18n.currentDicts("user.department");
	var initDataPromise = UserService.getInitData($state.params.id).then(function(data){
		vm.roleList = data.roleList;
	});
	
	vm.title="user.xinzeng";
	if(!ISS.isEmpty($state.params.id)){
		vm.isEdit = true;
		vm.title="user.xiugaiyonghu";
		vm.user.id = $state.params.id;
		initDataPromise.then(function(){
			UserService.initUpdate($state.params.id).then(function(data){
				if(data.success === false){
					dialogs.notify(data);
					cancel();
				}else{
					vm.user = data;
					vm.selectRoles = [];
					angular.forEach(vm.roleList, function(u){
						if(u.checked){
							vm.selectRoles.push(u);
						}
					});
				}
			},function(){
				cancel();
			});
		})
	}

	vm.treeObj = {};
	vm.treeSetting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback : {
			onAsyncSuccess: function(){
				onClickNode();
			},
			onCheck : onClickNode
		}
	};

	//回调
	function onClickNode(event, treeId, treeNode)
	{
		vm.user.archs = [];
	 	var checkedNodes = vm.treeObj.getCheckedNodes(true);
		angular.forEach(checkedNodes, function(node) {
			var area = {id: node.id};
			if (node.id != 0){
				vm.user.archs.push(area);
			}
		});
	}
	
	$http.post(ISS.getPath('/user/treeArea?userId=' + vm.user.id)).then(function(resp){
		if(ISS.isNotEmpty(resp.data))
		{
			var root = {id:0,name:ISS.getMessage('area.rootName')};
			angular.forEach(resp.data, function(node) {
				if (node.checked){
					root.checked = true;
				}
			});
			resp.data.push(root);
			vm.treeObj.reRender(vm.treeSetting, resp.data);
		}		
	});
	
	vm.cancel = function(){
		cancel();
	};
	
	function processData(){
		//角色
		vm.user.roles = [];
		angular.forEach(vm.selectRoles, function(role){
			vm.user.roles.push(angular.copy(role));
		});
	}
	// 提交数据
	vm.save = function(){
		
		processData();
		
		if(ISS.isEmpty(vm.user.id)){
			UserService.add(vm.user)
			.then(function(data){
				if(data.success){
					dialogs.notify(data).closed.then(function(){
						cancel();
					});
				}else{
					dialogs.notify(data);
				}	
			});
		}else{
			UserService.update(vm.user)
			.then(function(data){
				if(data.success){
					dialogs.notify(data).closed.then(function(){
						cancel();
					});
				}else{
					dialogs.notify(data);
				}	
			});
		}
	};


    }]);

app.controller('UserResetPwdCtrl', function($scope, $http, $timeout, $uibModalInstance, dialogs, UserService, data){
	var vm = $scope.vm = this;
	vm.user = data;
	
	vm.cancel = function(){
		$uibModalInstance.dismiss('Canceled');
	};
	
	function close(){
		$timeout(function(){
			$uibModalInstance.close(vm.user);
		}, 500);
	}
	
	// 提交数据
	vm.save = function(){
		UserService.resetPwd1(vm.user).then(function(data){
			vm.msgType = 'success';
			if(data.success == true){
				vm.errorMsg = getMessage('user.chongzhimimachenggong');
				close();
			}else if(data.results == "sendMailClose"){
				vm.errorMsg = getMessage('common.op.success');
				close();
			}else{
				vm.msgType = 'danger';
				vm.errorMsg = data.description;
				if(data.code === '00100'){
					vm.msgType = 'warning';
					close();
				}
			}
		})
	};
			
	
});
